package com.bussiness.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bussiness.auto.JSONAuthentication;
import com.bussiness.entity.BaApartment;
import com.bussiness.entity.BaFbtask;
import com.bussiness.mapper.BaFbtaskMapper;
import com.bussiness.mapper.BuUserMapper;
import com.bussiness.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bussiness
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/ba-fbtask")
public class BaFbtaskController extends JSONAuthentication {
    @Autowired
    BaFbtaskMapper baFbtaskMapper;
    @Autowired
    BuUserMapper buUserMapper;
    @ApiOperation(value = "添加任务")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody BaFbtask baFbtask) throws  Exception{
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        baFbtask.setCreatTime(timestamp);
            if(baFbtaskMapper.insert(baFbtask)>0){
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除任务")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=baFbtaskMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改任务")
    @PostMapping("/upd")
    public void updtast(HttpServletRequest request, HttpServletResponse response, @RequestBody BaFbtask baFbtask) throws  Exception{
        if(baFbtaskMapper.updateById(baFbtask)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改任务为完成")
    @PostMapping("/updtastWc")
    public void updtastWc(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws  Exception{
        BaFbtask baFbtask = baFbtaskMapper.selectById(id);
        baFbtask.setStatus("已完成");
        if(baFbtaskMapper.updateById(baFbtask)>0){
            Result ok = Result.ok().message("任务已完成");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找任务通过接收用户id")
    @PostMapping("/findAlltastByName")
    public void findAlltastByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("js_id",userId);
        Page<BaFbtask> shUserPage=new Page<>(current,size);
        Page<BaFbtask> shUserPages=baFbtaskMapper.selectPage(shUserPage,wrapper);
        List<BaFbtask> records = shUserPages.getRecords();
        int total=baFbtaskMapper.selectCount(wrapper);

        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找任务通过发布用户id")
    @PostMapping("/findAlltastByFsId")
    public void findAlltastByFsId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("fb_id",userId);
        Page<BaFbtask> shUserPage=new Page<>(current,size);
        Page<BaFbtask> shUserPages=baFbtaskMapper.selectPage(shUserPage,wrapper);
        List<BaFbtask> records = shUserPages.getRecords();
        int total=baFbtaskMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有任务")
    @PostMapping("/findAlltask")
    public void findAlltask(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<BaFbtask> shUserPage=new Page<>(current,size);
        Page<BaFbtask> shUserPages=baFbtaskMapper.selectPage(shUserPage,null);
        List<BaFbtask> records = shUserPages.getRecords();
        int total=baFbtaskMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("task",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

